package com.resource.manager.search;

import com.resource.manager.search.vo.FileType;
import com.resource.manager.search.vo.FileTypeEntity;
import com.resource.manager.search.vo.MainSearchEntity;
import com.resource.manager.util.BeanCopyUtil;
import com.resource.manager.util.FileSearchUtil;
import com.resource.manager.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.List;

/**
 * 搜索进度条窗口
 */
public class SearchScheFrame extends JFrame {

    private NewSearchWindow.SearchFileTypePanel searchFileTypePanel;
    private NewSearchWindow.ControlFileTypePanel controlFileTypePanel;
    private MainSearchEntity entity;
    private JLabel complete;
    private float scheRate = 0;
    private JPanel schePanel;

    public SearchScheFrame(NewSearchWindow.SearchFileTypePanel searchFileTypePanel, NewSearchWindow.ControlFileTypePanel controlFileTypePanel) {
        super("搜索进度");
        this.searchFileTypePanel = searchFileTypePanel;
        this.controlFileTypePanel = controlFileTypePanel;
        SwingUtil.setTitleIco(this);
        initFrame();
        initSearchEntity();
        //开始搜索
        search();
    }

    private void initFrame() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //设置不可缩放
        setResizable(false);
        //获取屏幕大小
        //设置窗口大小为屏幕大小一半,居中显示
        setSize(460, 180);
        setLocationRelativeTo(null);
        //设置背景色为白色
        getContentPane().setBackground(Color.WHITE);
        this.setLayout(null);


        JLabel label = new JLabel("正在搜索文件...");
        label.setBounds(30, 10, 150, 20);
        label.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        this.add(label);

        complete = new JLabel("已完成 0%");
        complete.setBounds(30, 35, 150, 20);
        complete.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        this.add(complete);

        schePanel = new JPanel() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.setColor(Color.LIGHT_GRAY);
                g.fillRect(0, 0, 360, 12);
                g.setColor(new Color(3, 176, 36));
                g.fillRect(0, 0, (int) (3.6 * scheRate), 12);
                //设置进度条特效样式

            }
        };
        schePanel.setLayout(null);
        schePanel.setBounds(30, 65, 360, 12);
        this.add(schePanel);
        this.setVisible(true);
    }

    private void search() {
        new Thread(() -> {
            FileSearchUtil.searchFile(entity);
        }).start();
        new Thread(this::searchSche).start();
    }


    private void searchSche() {
        while (!entity.isSearchFlag()) {
            try {
                if (scheRate < 99) {
                    scheRate+=0.1f;
                }
                Thread.sleep(100);
                schePanel.repaint();
                complete.setText("已完成 " + (int)scheRate + "%");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        while (scheRate < 100) {
            try {
                scheRate +=1f;
                Thread.sleep(8);
                complete.setText("已完成 " +  (int)scheRate + "%");
                schePanel.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        scheRate = 100;
        complete.setText("已完成100%");
        schePanel.repaint();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //窗口关闭
        this.dispose();
        //打开主查询窗口
        openMainSearchWindow();
    }


    private void initSearchEntity() {
        if (searchFileTypePanel == null) {
            //测试
            MainSearchEntity mainSearchEntity = new MainSearchEntity();
            mainSearchEntity.setPath("D:\\");
            FileTypeEntity fileTypeEntity = new FileTypeEntity();
            fileTypeEntity.setSuffix(Arrays.asList(".png", ".jpg"));
            fileTypeEntity.setIco("img/image.png");
            fileTypeEntity.setName("图片");
            mainSearchEntity.setFileTypeEntitys(Arrays.asList(fileTypeEntity));
            entity = mainSearchEntity;
            return;
        }
        entity = new MainSearchEntity();
        entity.setSearchFlag(false);
        entity.setPath(controlFileTypePanel.pathLabel.getText());
        List<FileType> fileTypes = searchFileTypePanel.typeInstance.getFileTypes();
        List<FileTypeEntity> fileTypeEntities = BeanCopyUtil.copyList(fileTypes, FileTypeEntity.class);
        fileTypeEntities.get(0).setSCheckFlag(true);
        entity.setFileTypeEntitys(fileTypeEntities);
        entity.setAllFileTypeEntitys(fileTypeEntities);
    }


    private void openMainSearchWindow() {
        new MainSearchWindow(entity);
    }
}
